package com.jianjian.common.utils;

import io.jsonwebtoken.*;
import org.springframework.util.StringUtils;

import java.util.Date;

/**
 * 生成JSON Web令牌的工具类
 */
public class JwtHelper {
    //token过期时间
    private static long tokenExpiration = 365 * 24 * 60 * 60 * 1000;
    //加密密钥
    private static String tokenSignKey = "123456";

    /**
     * 根据用户id和用户名称生成token字符串
     * @param userId    用户id
     * @param username  用户名称
     * @return
     */
    public static String createToken(Long userId, String username) {
        String token = Jwts.builder()
                .setSubject("AUTH-USER")//分组
                .setExpiration(new Date(System.currentTimeMillis() + tokenExpiration))//设置过期时间

                .claim("userId", userId)
                .claim("username", username)

                .signWith(SignatureAlgorithm.HS512, tokenSignKey)//根据密钥进行编码加密处理
                .compressWith(CompressionCodecs.GZIP)//将字符串压缩 变成一行处理
                .compact();
        return token;
    }

    /**
     * 从token获取用户id
     * @param token 字符串
     * @return
     */
    public static Long getUserId(String token) {
        try {
            if (StringUtils.isEmpty(token)) return null;

            Jws<Claims> claimsJws = Jwts.parser().setSigningKey(tokenSignKey).parseClaimsJws(token);
            Claims claims = claimsJws.getBody();
            Integer userId = (Integer) claims.get("userId");
            return userId.longValue();
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 从token获取用户名
     * @param token 字符串
     * @return
     */
    public static String getUsername(String token) {
        try {
            if (StringUtils.isEmpty(token)) return "";

            Jws<Claims> claimsJws = Jwts.parser().setSigningKey(tokenSignKey).parseClaimsJws(token);
            Claims claims = claimsJws.getBody();
            return (String) claims.get("username");
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public static void removeToken(String token) {
        //jwttoken无需删除，客户端扔掉即可。
    }

    public static void main(String[] args) {
        //admin:  eyJhbGciOiJIUzUxMiIsInppcCI6IkdaSVAifQ.H4sIAAAAAAAAAKtWKi5NUrJScgwN8dANDXYNUtJRSq0oULIyNDOzNDE3MTIw01EqLU4t8kwBikGYeYm5qUAtiSm5mXlKtQAMYdZBQgAAAA.3kfQ3UJziKlsTwKTbGOb5ezf_0Tc4ueyDJt28068Rqs17HvDYgSou6e69_1F0kzNAi-gFTbJRcYcj5pn3WUQkQ
        //wanggang : eyJhbGciOiJIUzUxMiIsInppcCI6IkdaSVAifQ.H4sIAAAAAAAAAKtWKi5NUrJScgwN8dANDXYNUtJRSq0oULIyNDOzNDE3MTWw0FEqLU4t8kxRsjKGMPMSc1OBWsoT89LTgVipFgDspm8ORQAAAA.rKOAosK2uQNwLin9fW-BujEjDh0RpbiGMOHAv_uMi3C_Jk2EkhaCrJbJZ7vlM_rYo5RMhqvb_CZMZ0n_NE3dXA
        String token = JwtHelper.createToken(1L, "admin");//"eyJhbGciOiJIUzUxMiIsInppcCI6IkdaSVAifQ.H4sIAAAAAAAAAKtWKi5NUrJSCjAK0A0Ndg1S0lFKrShQsjI0MzY2sDQ3MTbQUSotTi3yTFGyMjKEsP0Sc1OBWp6unfB0f7NSLQDxzD8_QwAAAA.2eCJdsJXOYaWFmPTJc8gl1YHTRl9DAeEJprKZn4IgJP9Fzo5fLddOQn1Iv2C25qMpwHQkPIGukTQtskWsNrnhQ";//JwtHelper.createToken(7L, "admin");
        System.out.println(token);
        System.out.println(JwtHelper.getUserId(token));
        System.out.println(JwtHelper.getUsername(token));
    }
}